package com.lzw.bluetoothpractice.Fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.lzw.bluetoothpractice.Adapters.RecoveryAction2ListAdapter;
import com.lzw.bluetoothpractice.Adapters.RecoveryActionListAdapter;
import com.lzw.bluetoothpractice.Adapters.VideoAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.MyApplication;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.Utils.TimeUtils;
import com.lzw.bluetoothpractice.bean.PlanBean;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.ArrayList;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class StageTargetFragment extends Fragment {
    public StageTargetFragment() {
    }
    private Map<String, String> keyMap;



    private ListView listView;
    private ArrayList<String> datas;
    private JCVideoPlayerStandard currPlayer;
    private VideoAdapter adapter;
    private AbsListView.OnScrollListener onScrollListener;

    Button angle;
    Button temperture;
    Button distance;

    RecyclerView rvTodayAction2List;
    RecoveryAction2ListAdapter recoveryAction2ListAdapter;
    private int firstVisible;//当前第一个可见的item
    private int visibleCount;//当前可见的item个数
    private Handler mHander=new Handler();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.stage_target,container,false);
        rvTodayAction2List=v.findViewById(R.id.rv_plan2List);


        initPlan();

        initDatas();
        //initListener();
        return v;
    }

    private void initPlan(){
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/doctor/list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;
        String userid = SharePreferenceUtils.getString(getContext(),"userId",null);
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).getPlan(Long.parseLong("2"),"1", AppUtil.getVersionCode() + "", accessToken ,keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("计划2：",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {

                PlanBean planBean = JSON.parseObject(json.toString(), PlanBean.class);
                if(planBean.getCode()==200){

                    //Article article=new Article();
                    LinearLayoutManager linearLayoutManager2=new LinearLayoutManager(getContext());
                    linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
                    rvTodayAction2List.setLayoutManager(linearLayoutManager2);
                    //   rvTodayActionList.addItemDecoration(new DividerItemDecoration(mcontext,DividerItemDecoration.VERTICAL));
                    recoveryAction2ListAdapter=new RecoveryAction2ListAdapter(getContext());
                    recoveryAction2ListAdapter.updateData(planBean.getData());
                    rvTodayAction2List.setAdapter(recoveryAction2ListAdapter);



                }else {
                    Toast toast=Toast.makeText(getContext(),"查询我的医生失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });

    }
    private void initDatas() {


    }

    /**
     * 滑动监听
     */
   /* private void initListener() {
        onScrollListener = new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

                switch (scrollState) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                        break;

                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                        //滑动停止自动播放视频
                        autoPlayVideo(view);
                        break;

                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisible == firstVisibleItem) {
                    return;
                }

                firstVisible = firstVisibleItem;
                visibleCount = visibleItemCount;
            }
        };

        listView.setOnScrollListener(onScrollListener);
    }*/

    /**
     * 滑动停止自动播放视频
     */
   /* private void autoPlayVideo(AbsListView view) {

        for (int i = 0; i < visibleCount; i++) {
            if (view != null && view.getChildAt(i) != null && view.getChildAt(i).findViewById(R.id.player_list_video) != null) {
                currPlayer = (JCVideoPlayerStandard) view.getChildAt(i).findViewById(R.id.player_list_video);
                Rect rect = new Rect();
                //获取当前view 的 位置
                currPlayer.getLocalVisibleRect(rect);
                int videoheight = currPlayer.getHeight();
                if (rect.top == 0 && rect.bottom == videoheight) {
                    if (currPlayer.currentState == JCVideoPlayer.CURRENT_STATE_NORMAL
                            || currPlayer.currentState == JCVideoPlayer.CURRENT_STATE_ERROR) {
                        currPlayer.startButton.performClick();
                    }
                    return;
                }
            }
        }
        //释放其他视频资源
        JCVideoPlayer.releaseAllVideos();
    }
*/
    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
}
